﻿using Logger.Service.Interface;
using log4net;
using System.Reflection;

namespace Logger.Service
{
    /// <summary>
    /// Log4net日志
    /// </summary>
    public class Log4netService : ILoggerContext
    {
        private readonly log4net.ILog _logger;

        public Log4netService() : this(MethodBase.GetCurrentMethod().DeclaringType)
        {
        }

        public Log4netService(Type type) : this(type, string.Empty)
        {
        }

        public Log4netService(Type type, string configPath)
        {
            if (string.IsNullOrEmpty(configPath))
            {
                log4net.Config.XmlConfigurator.Configure();
            }
            else
            {
                log4net.Config.XmlConfigurator.Configure(new FileInfo(configPath));
            }
            _logger = LogManager.GetLogger(type);
        }

        public void Debug(string msg, params object[] args)
        {
            _logger.DebugFormat(msg, args);
        }

        public void Debug(string msg, Exception err)
        {
            _logger.Debug(msg, err);
        }

        public void Info(string msg, params object[] args)
        {
            _logger.InfoFormat(msg, args);
        }

        public void Info(string msg, Exception err)
        {
            _logger.Info(msg, err);
        }

        public void Warn(string msg, params object[] args)
        {
            _logger.WarnFormat(msg, args);
        }

        public void Warn(string msg, Exception err)
        {
            _logger.Warn(msg, err);
        }

        public void Error(string msg, params object[] args)
        {
            _logger.ErrorFormat(msg, args);
        }

        public void Error(string msg, Exception err)
        {
            _logger.Error(msg, err);
        }

    }
}
